<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
request.setCharacterEncoding("UTF-8");
String htmlData = request.getParameter("content1") != null ? request.getParameter("content1") : "";
%>
<html>
<head>
<jsp:include page="../common/include.jsp"></jsp:include>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
	<%
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
	%>
	<base href="<%=basePath%>">
	<link rel="icon" href="favicon.ico" type="image/x-icon" />
	<link href="<%=basePath%>/template/admin/css/input.css" rel="stylesheet" type="text/css" />
	<!-- 有关kindEditor插件的js和css -->
	<link rel="stylesheet" href="<%=basePath%>template/kindEditor/themes/default/default.css" />
	<link rel="stylesheet" href="<%=basePath%>template/kindEditor/plugins/code/prettify.css" />
	<script charset="utf-8" src="<%=basePath%>template/kindEditor/kindeditor.js"></script>
	<script charset="utf-8" src="<%=basePath%>template/kindEditor/lang/zh_CN.js"></script>
	<script charset="utf-8" src="<%=basePath%>template/kindEditor/plugins/code/prettify.js"></script>
	<!-- 有关swfUpload图片上传插件的js和css -->
	<link href="<%=basePath%>template/swfupload/css/swfupload-default.css" rel="stylesheet"type="text/css" />
	<script type="text/javascript" src="<%=basePath%>template/swfupload/js/jquery-latest.js"></script>
	<script type="text/javascript" src="<%=basePath%>template/swfupload/js/swfupload.js"></script>
	<script type="text/javascript" src="<%=basePath%>template/swfupload/js/handlers.js"></script>
	
	<script type="text/javascript">
	$().ready(function() {

		// 查询商品属性
		$("#productTypeId").change( function() {
			$(".productAttributeContentTr").remove();
			var productTypeId = $("#productTypeId").val();
			$.ajax({
				url: "product_attribute!ajaxProductAttribute.action",
				dataType: "json",
				data:{productTypeId: productTypeId},
				async: false,
				success: function(json) {
					var productAttributeTrHtml = "";
					$.each(json, function(i) {
						if(json[i]["attributeType"] == "text") {
							productAttributeTrHtml += '<tr class="productAttributeContentTr"><th>' + json[i].name + ':</th><td><input type="text" name="' + json[i].id + '"' + ((json[i].isRequired == true) ? ' class="formText {required: true}"' : ' class="formText"') + ' />' + ((json[i].isRequired == true) ? '<label class="requireField">*</label>' : '') + '</td></tr>';
						} else if(json[i]["attributeType"] == "number") {
							productAttributeTrHtml += '<tr class="productAttributeContentTr"><th>' + json[i].name + ':</th><td><input type="text" name="' + json[i].id + '"' + ((json[i].isRequired == true) ? ' class="formText {required: true, number: true}"' : ' class="formText {number: true}"') + ' />' + ((json[i].isRequired == true) ? '<label class="requireField">*</label>' : '') + '</td></tr>';
						} else if(json[i]["attributeType"] == "alphaint"){
							productAttributeTrHtml += '<tr class="productAttributeContentTr"><th>' + json[i].name + ':</th><td><input type="text" name="' + json[i].id + '"' + ((json[i].isRequired == true) ? ' class="formText {required: true, lettersonly: true}"' : ' class="formText {lettersonly: true}"') + ' />' + ((json[i].isRequired == true) ? '<label class="requireField">*</label>' : '') + '</td></tr>';
						} else if(json[i]["attributeType"] == "select") {
							var productAttributeOption = '<option value="">请选择...</option>';
							for(var key in json[i]["attributeOptionList"]) {
								productAttributeOption += ('<option value="' + json[i]["attributeOptionList"][key] + '">' + json[i]["attributeOptionList"][key] + '</option>');
							}
							productAttributeTrHtml += '<tr class="productAttributeContentTr"><th>' + json[i].name + ':</th><td><select name="' + json[i].id + '"' + ((json[i].isRequired == true) ? ' class="{required: true}"' : '') + '>' + productAttributeOption + '</select>' + ((json[i].isRequired == true) ? '<label class="requireField">*</label>' : '') + '</td></tr>';
						} else if(json[i]["attributeType"] == "checkbox") {
							var productAttributeOption = "";
							for(var key in json[i]["attributeOptionList"]) {
								productAttributeOption += ('<label><input type="checkbox" name="' + json[i].id + '" value="' + json[i]["attributeOptionList"][key] + '"' + ((json[i].isRequired == true) ? ' class="{required: true, messagePosition: \'#' + json[i].id + 'MessagePosition\'}"' : '') +' />' + json[i]["attributeOptionList"][key] + '</label>&nbsp;&nbsp;');
							}
							productAttributeTrHtml += '<tr class="productAttributeContentTr"><th>' + json[i].name + ':</th><td>' + productAttributeOption + ((json[i].isRequired == true) ? '<span id="' + json[i].id + 'MessagePosition"></span><label class="requireField">*</label>' : '') + '</td></tr>';
						} else if(json[i]["attributeType"] == "date") {
							productAttributeTrHtml += '<tr class="productAttributeContentTr"><th>' + json[i].name + ':</th><td><input type="text" name="' + json[i].id + '"' + ((json[i].isRequired == true) ? ' class="formText datePicker {required: true, dateISO: true}"' : ' class="formText datePicker {dateISO: true}"') + ' />' + ((json[i].isRequired == true) ? '<label class="requireField">*</label>' : '') + '</td></tr>';
						}
					});
					$("#productTypeTr").after(productAttributeTrHtml);
					$.bindDatePicker();
				}
			});
		});
	
		

	});
</script>

	<script>
		KindEditor.ready(function(K) {
			var editor1 = K.create('textarea[name="description"]', {
				cssPath : 'template/kindEditor/plugins/code/prettify.css',
				uploadJson : 'template/kindEditor/upload_json.jsp',
				fileManagerJson : 'template/kindEditor/file_manager_json.jsp',
				allowFileManager : true,
				afterCreate : function() {
					var self = this;
					K.ctrl(document, 13, function() {
						self.sync();
						document.forms['example'].submit();
					});
					K.ctrl(self.edit.doc, 13, function() {
						self.sync();
						document.forms['example'].submit();
					});
				}
			});
			prettyPrint();
		});
	</script>
	
	<script type="text/javascript">
	var contextPath="${pageContext.request.contextPath}";
		function startLoad(){
			var url=contextPath+"/template/swfupload/fileUpload.jsp"; //处理上传的servlet
			var sizeLimit="2MB";// 文件的大小  注意: 中间要有空格
			var types="*.jpg;*.jpeg;*.gif"; //注意是 " ; " 分割 
			var typesdesc="web image file"; //这里可以自定义
			var uploadLimit=10;  //上传文件的 个数
			initSwfupload(url,sizeLimit,types,typesdesc,uploadLimit);
		}
	</script>
	<style type="text/css">
		#uploadBody .ke-dialog-content{
			width: 610px;
		}
	</style>
</head>
<body class="input">
	<div class="body">
		<c:set value="添加商品" var="title" />
		<c:set value="admin/product/productAdd" var="actionPath" />
		<c:if test="${IsUpdate==true}">
				<c:set value="admin/product/productUpdate" var="actionPath" />
				<c:set value="更新商品" var="title" />
		</c:if>
		
		
		<div class="inputBar">
			<h1><span class="icon">&nbsp;</span>${title}</h1>
		</div>
		<div class="blank"></div>
		<!-- action="<#if isAdd??>product!save.action<#else>product!update.action</#if>" -->
		
		
		<sf:form id="inputForm" class="validate" action="${actionPath}" method="post" enctype="multipart/form-data" modelAttribute="product" >
			<input type="hidden" name="id" value="${product.id}" />
			<ul class="tab">
				<li>
					<input type="button" value="基本信息" hidefocus="true" />
				</li>
				<li>
					<input type="button" value="商品描述" hidefocus="true" />
				</li>
				<li>
					<input type="button" value="商品属性" hidefocus="true" />
				</li>
			</ul>
			<table class="inputTable tabContent">
				<tr>
					<th>
						商品名称:
					</th>
					<td>
						<input type="text" name="name" class="formText {required: true}" value="${(product.name)}" />
						<label class="requireField">*</label>
					</td>
				</tr>
				<tr>
					<th>
						货号:
					</th>
					<td>
						<input type="text" class="formText" name="productSn" value="${(product.productSn)}" title="若留空则由系统随机生成" />
					</td>
				</tr>
				<tr>
					<th>
						商品分类:
					</th>
					<td>
						<select name="productCategory.id" class="{required: true}">
							<option value="">请选择...</option>
							<%-- <#list productCategoryTreeList as list>
								<option value="${list.id}"<#if (list.id == product.productCategory.id)!> selected</#if>>
									<#if list.level != 0>
										<#list 1..list.level as i>------</#list>
									</#if>
									${list.name}
								</option>
							</#list> --%>
							<c:forEach var="item" items="${productCategory}">
								<c:if test="${item.id==product.productCategory.id}">  
									<option value="${item.id}" selected="selected" >${item.name}</option>
								</c:if>
								 <option value="${item.id}">${item.name}</option>
							</c:forEach>
						</select>
					</td>
				</tr>
				<tr>
					<th>
						商品品牌:
					</th>
					<td>
						<select name="brand.id">
							<option value="">请选择...</option>
							<%-- <#list allBrand as list>
								<option value="${list.id}"<#if (list.id == product.brand.id)!> selected </#if>>
									${list.name}
								</option>
							</#list> --%>
							<c:forEach var="item" items="${brand}">
								<c:if test="${item.id==product.brand.id}">  
									<option value="${item.id}" selected="selected" >${item.name}</option>
								</c:if>
								 <option value="${item.id}" >${item.name}</option>
							</c:forEach>
						</select>
					</td>
				</tr>
				<tr>
					<th>
						本店售价:
					</th>
					<td>
						<input type="text" name="price" class="formText {required: true, min: 0}" value="${(product.price)}" />
						<label class="requireField">*</label>
					</td>
				</tr>
				<tr>
					<th>
						市场售价:
					</th>
					<td>
						<input type="text" name="marketPrice" class="formText {required: true, min: 0}" value="${(product.marketPrice)}" />
						<label class="requireField">*</label>
					</td>
				</tr>
				<%-- <#if systemConfig.pointType == "productSet">
					<tr>
						<th>
							积分:
						</th>
						<td>
							<input type="text" name="product.point" class="formText {required: true, digits: true}" value="${(product.point)!"0"}" />
						</td>
					</tr>
				</#if> --%>
				<tr>
					<th>
						商品重量:
					</th>
					<td>
						<input type="text" name="weight" class="formText {required: true, min: 0, messagePosition: '#weightMessagePosition'}" value="${(product.weight)}" title="0表示不计重量" />
						<select name="weightUnit">
							<%-- <#list allWeightUnit as list>
								<option value="${list}"<#if (list == product.weightUnit)!> selected </#if>>
									${action.getText("WeightUnit." + list)}
								</option>
							</#list> --%>
						</select>
						<span id="weightMessagePosition"></span>
						<label class="requireField">*</label>
					</td>
				</tr>
				<tr>
					<th>
						库存量:
					</th>
					<td>
						<input type="text" name="store" class="formText {digits: true}" value="${(product.store)}" title="只允许输入零或正整数，为空表示不计库存" />				 						
					</td>
				</tr>
				<tr>
					<th>
						是否精品推荐:
					</th>
					<td>
						<c:if test="${product.isHot == true}">  
							<label><input type="radio" name="isBest" value="true" checked/>是</label>
							<label><input type="radio" name="isBest" value="false"/>否</label>
						</c:if>
						
						<c:if test="${product.isHot != true}">  
							<label><input type="radio" name="isBest" value="true"/>是</label>
							<label><input type="radio" name="isBest" value="false" checked/>否</label>
						</c:if>
						 
					</td>
				</tr>
				<tr>
					<th>
						是否新品推荐:
					</th>
					<td>
						<c:if test="${product.isNew == true}">  
							<label><input type="radio" name="isNew" value="true" checked/>是</label>
							<label><input type="radio" name="isNew" value="false"/>否</label>
						</c:if>
						<c:if test="${product.isNew != true}">  
							<label><input type="radio" name="isNew" value="true"/>是</label>
							<label><input type="radio" name="isNew" value="false" checked/>否</label>
						</c:if>
						
					</td>
				</tr>
				<tr>
					<th>
						是否热销推荐:
					</th>
					<td>
						<c:if test="${product.isHot == true}">  
							<label><input type="radio" name="isHot" value="true" checked/>是</label>
							<label><input type="radio" name="isHot" value="false"/>否</label>
						</c:if>
						
						<c:if test="${product.isHot != true}">  
							<label><input type="radio" name="isHot" value="true"/>是</label>
							<label><input type="radio" name="isHot" value="false" checked/>否</label>
						</c:if>
						
					</td>
				</tr>
				<tr>
					<th>
						是否上架:
					</th>
					<td>
						<c:if test="${product.isMarketable == true}">  
							<label><input type="radio" name="isMarketable" value="true" checked/>是</label>
							<label><input type="radio" name="isMarketable" value="false"/>否</label>
						</c:if>
						
						<c:if test="${product.isMarketable != true}">  
						<label><input type="radio" name="isMarketable" value="true"/>是</label>
							<label><input type="radio" name="isMarketable" value="false" checked/>否</label>
						</c:if>
						
					</td>
				</tr>
				<tr>
					<th>
						上传商品图片
					</th>
					<td>
						<div class="productImageArea">
							<input id="picUpload" type="button" onclick="startLoad()" value="批量图片上传"/><br/>
							<c:if test="${IsUpdate==true}">
								<c:forEach var="item" items="${productImages}">
									<div style="height: 30px;">
										<input type="text" value="${item.sourceProductImagePath}" name="uploadPicPath" style="width:300px;">
										<img alt="" src="${item.sourceProductImagePath}" style="width:30px;"><br/>
									 </div>
								</c:forEach>
							</c:if>
						</div>
					</td>
				</tr>
				<tr>
					<th>
						页面关键词:
					</th>
					<td>
						<input type="text" name="metaKeywords" class="formText" value="${(product.metaKeywords)}" />
					</td>
				</tr>
				<tr>
					<th>
						页面描述:
					</th>
					<td>
						<input type="text" name="metaDescription" class="formText" value="${(product.metaDescription)}" />
					</td>
				</tr>
				<tr>
					<th>
						商品描述:
					</th>
					<td>
						<%-- <textarea name="description" cols="100" rows="8" style="width:700px;height:200px;visibility:hidden;"><%=htmlspecialchars(htmlData)%></textarea> --%>
						<textarea name="description" cols="100" rows="8" style="width:700px;height:200px;visibility:hidden;">${(product.description)}</textarea>
					</td>
				</tr>
			</table>
			<table class="inputTable tabContent">
				<tr id="productTypeTr">
					<th>
						商品类型:
					</th>
					<td>
						<select name="productType.id" id="productTypeId">
							<option value="">请选择...</option>
							<%-- <#list allProductType as list>
								<option value="${list.id}"<#if (list.id == product.productType.id)!> selected </#if>>${list.name}</option>
							</#list> --%>
						</select>
					</td>
				</tr>
				<%-- <#list (product.productType.enabledProductAttributeList)! as list>
					<tr class="productAttributeContentTr">
						<th>
							${list.name}:
						</th>
						<td>
							<#if list.attributeType == "text">
								<input type="text" name="${list.id}" class="formText<#if list.isRequired> {required: true}</#if>" value="${(product.productAttributeMap.get(list)[0])!}" />
								<#if list.isRequired><label class="requireField">*</label></#if>
							<#elseif list.attributeType == "number">
								<input type="text" name="${list.id}" class="formText {<#if list.isRequired>required: true, </#if>number: true}" value="${(product.productAttributeMap.get(list)[0])!}" />
								<#if list.isRequired><label class="requireField">*</label></#if>
							<#elseif list.attributeType == "alphaint">
								<input type="text" name="${list.id}" class="formText {<#if list.isRequired>required: true, </#if>lettersonly: true}" value="${(product.productAttributeMap.get(list)[0])!}" />
								<#if list.isRequired><label class="requireField">*</label></#if>
							<#elseif list.attributeType == "select">
								<select name="${list.id}"<#if list.isRequired> class="{required: true}"</#if>>
									<option value="">请选择...</option>
									<#list list.attributeOptionList as attributeOptionList>
										<option value="${attributeOptionList}"<#if (product.productAttributeMap.get(list)[0] == attributeOptionList)!> selected</#if>>${attributeOptionList}</option>
									</#list>
								</select>
								<#if list.isRequired><label class="requireField">*</label></#if>
							<#elseif list.attributeType == "checkbox">
								<#list list.attributeOptionList as attributeOptionList>
									<label><input type="checkbox" name="${list.id}"<#if list.isRequired> class="{required: true, messagePosition: '#${list.id}MessagePosition'}"</#if> value="${attributeOptionList}"<#if (product.productAttributeMap.get(list).contains(attributeOptionList))!> checked</#if> />${attributeOptionList}</label>
								</#list>
								<span id="${list.id}MessagePosition"></span>
								<#if list.isRequired><label class="requireField">*</label></#if>
							<#elseif list.attributeType == "date">
								<input type="text" name="${list.id}" class="formText datePicker {<#if list.isRequired>required: true, </#if>dateISO: true}" value="${(product.productAttributeMap.get(list)[0])!}" />
								<#if list.isRequired><label class="requireField">*</label></#if>
							</#if>
						</td>
					</tr>
				</#list> --%>
			</table>
			<div class="buttonArea">
				<input type="submit" class="formButton" value="确  定" hidefocus="true" />&nbsp;&nbsp;&nbsp;&nbsp;
				<input type="button" class="formButton" onclick="window.history.back(); return false;" value="返  回" hidefocus="true" />
			</div>
		</sf:form>
		
	</div>
</body>
</html>
<%!
private String htmlspecialchars(String str) {
	str = str.replaceAll("&", "&amp;");
	str = str.replaceAll("<", "&lt;");
	str = str.replaceAll(">", "&gt;");
	str = str.replaceAll("\"", "&quot;");
	return str;
}
%>